package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.SeatListDao;
import person.aoao.library.dao.TimeListDao;
import person.aoao.library.dao.UserListDao;
import person.aoao.library.dao.WillListDao;
import person.aoao.library.entity.TimeList;
import person.aoao.library.entity.UserList;
import person.aoao.library.entity.WillList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.SeatListService;
import person.aoao.library.service.WillListService;
import person.aoao.library.vo.ListData;
import person.aoao.library.vo.SeatView;
import person.aoao.library.vo.TimePlus;
import person.aoao.library.vo.ViewCount;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (WillList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:40
 */
@Service("willListService")
public class WillListServiceImpl implements WillListService {
    @Resource
    private WillListDao willListDao;
    @Resource
    private SeatListDao seatListDao;
    @Resource
    SeatListService seatListService;
    @Resource
    private UserListDao userListDao;
    @Resource

    private TimeListDao timeListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param willid 主键
     * @return 实例对象
     */
    @Override
    public WillList queryById(Integer willid) {
        return this.willListDao.queryById(willid);
    }

    /**
     * 分页查询
     *
     * @param willList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<WillList> queryByPage(WillList willList, PageRequest pageRequest) {
        long total = this.willListDao.count(willList);
        return new PageImpl<>(this.willListDao.queryAllByLimit(willList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param willList 实例对象
     * @return 实例对象
     */
    @Override
    public WillList insert(WillList willList) {
        willList.setState("0");
        System.out.println(willList.getState());
        UserList user=this.userListDao.getUserInfoByUid(willList.getUid());
        if(user==null){
            return null;
        }
        System.out.println(user.getUid());
        this.willListDao.insert(willList);
        return willList;
    }

    /**
     * 修改数据
     *
     * @param willList 实例对象
     * @return 实例对象
     */
    @Override
    public WillList update(WillList willList) {
        this.willListDao.update(willList);
        return this.queryById(willList.getWillid());
    }

    /**
     * 通过主键删除数据
     *
     * @param willid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer willid) {
        return this.willListDao.deleteById(willid) > 0;
    }

    @Override
    public List<String> isWill(String seatId, String date) {
        List<WillList> list=this.willListDao.isWill(seatId,date);
        List<String> times= new ArrayList<>();
        for(WillList item:list){
            System.out.println(item.getTimeperiod());
            times.add(item.getTimeperiod());
        }
        return times;
    }

    @Override
    public List<WillList> getOreders(String uid, Integer page, Integer num) {
        List<WillList> list=this.willListDao.getOrders(uid,(page-1)*num,num);
        for(WillList item:list){
            item.setSeatList(this.seatListService.queryById(item.getSeatid()));
            item.setTimeList(this.timeListDao.queryById(Integer.parseInt(item.getTimeperiod())));
        }
        return list;
    }

    @Override
    public ListData checkInfo(String key) {
        return null;
    }

    @Override
    public ResponseData getWillListByUid(String uid, Integer num, Integer page) {
        ListData data=new ListData();
        List<WillList> list=this.willListDao.getOrders(uid,(page-1)*num,num);
        for(WillList item:list){
            item.setSeatList(this.seatListService.queryById(item.getSeatid()));
            item.setTimeList(this.timeListDao.queryById(Integer.parseInt(item.getTimeperiod())));
        }
        long count=this.willListDao.getCountByUid(uid);
        data.setCount(count);
        data.setData(list);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getWillList(Integer num, Integer page) {
        ListData data=new ListData();
        List<WillList> list=this.willListDao.getWillList(num,(page-1)*num);
        for(WillList item:list){
            System.out.println(item.getTimeperiod());
            item.setTimeList(this.timeListDao.queryById(Integer.parseInt(item.getTimeperiod())));
            item.setSeatList(this.seatListService.queryById(item.getSeatid()));
            item.setUserList(this.userListDao.queryByUid(item.getUid()));
        }
        long count=this.willListDao.getCount();
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData searchWillList(String str, Integer state,Integer num, Integer page ) {
        ListData data=new ListData();
        List<WillList> list=this.willListDao.searchWillList("%"+str+"%",state,num,(page-1)*num);
        for(WillList item:list){
            System.out.println(item.getTimeperiod());
            item.setTimeList(this.timeListDao.queryById(Integer.parseInt(item.getTimeperiod())));
            item.setSeatList(this.seatListService.queryById(item.getSeatid()));
            item.setUserList(this.userListDao.queryByUid(item.getUid()));
        }
        long count=this.willListDao.getSearchCount("%"+str+"%",state);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getWillListByUidState(String uid, Integer state, Integer num, Integer page) {
            ListData data=new ListData();
            List<WillList> list=this.willListDao.getWillListByUidState(uid,state,num,(page-1)*num);
        for(WillList item:list){
            item.setSeatList(this.seatListService.queryById(item.getSeatid()));
            item.setTimeList(this.timeListDao.queryById(Integer.parseInt(item.getTimeperiod())));
        }
        long count=this.willListDao.getCountByUidStateS(uid,state);
        data.setCount(count);
        data.setData(list);
        return new ResponseData(ResponseCode.SUCCESS,data);

    }

    @Override
    public ResponseData getTimePlus(TimePlus queey) {
        List<TimeList> timelist = this.timeListDao.getTimePeriod();
        for (TimeList time : timelist) {
            time.setState(true);
        }
        System.out.println(queey.getSeatid());
        List<WillList> willlist = this.willListDao.getTimePlus(queey.getSeatid(), queey.getRetruntime() + " 08:00:00");
        for (WillList will : willlist) {
            for (TimeList time : timelist) {
                if(time.getState()){
                    if (Integer.parseInt(will.getTimeperiod()) == time.getTimeid()) {
                        time.setState(false);
                    }else{
                        time.setState(true);
                    }
                }
            }
        }
        return new ResponseData(ResponseCode.SUCCESS,timelist);
    }

    @Override
    public ResponseData weekData(ArrayList<String> weeks) {
        System.out.println(this.willListDao.getWillCountByDate("2023-05-22%"));
       SeatView seatViews=new SeatView();
       seatViews.setTitle("预约次数");
        seatViews.setCount(this.seatListDao.getCount());
        ArrayList<ViewCount> list=new ArrayList<>();

        SeatView useSeatViews=new SeatView();
        useSeatViews.setTitle("使用次数");
        useSeatViews.setCount(this.seatListDao.getCount());
        ArrayList<ViewCount> uselist=new ArrayList<>();

        for(String date:weeks){
            ViewCount v=new ViewCount();
            long count=this.willListDao.getWillCountByDate(date+"%");
            System.out.println(count+" "+date);
            v.setDate(date);
            v.setNum(count);
            list.add(v);

            ViewCount usev=new ViewCount();
            long useCount=this.willListDao.getWillCountUse(date+"%");
            usev.setDate(date);
            usev.setNum(useCount);
            uselist.add(usev);

        }
        seatViews.setWeek(list);
        useSeatViews.setWeek(uselist);
        ArrayList data=new ArrayList<>();
        data.add(seatViews);
        data.add(useSeatViews);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

}
